#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::pair;
using std::queue;
using std::vector;

const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
const int inf = (int)1e9;

int main() {
	int m, n;
	cin >> m >> n;
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	--x1, --y1, --x2, --y2;
	vector <vector <int> > a(m, vector <int>(n, 0));
	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> a[i][j];
		}
	}
	queue <pair <int, int> > q;
	q.push({ x1, y1 });
	vector <vector <int> > d(m, vector <int>(n, inf));
	d[x1][y1] = 0;
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; ++i) {
			if (!(x + dx[i] >= 0 && x + dx[i] < m && y + dy[i] >= 0 && y + dy[i] < n))
				continue;
			if (a[x + dx[i]][y + dy[i]])
				continue;
			if (d[x + dx[i]][y + dy[i]] <= d[x][y] + 1)
				continue;
			d[x + dx[i]][y + dy[i]] = d[x][y] + 1;
			q.push({ x + dx[i], y + dy[i] });
		}
	}
	if (d[x2][y2] == inf) {
		cout << "NO";
	} else {
		cout << d[x2][y2];
	}
}